<template>
  <div>
    <div class="post">
      <div v-if="isLoading" class="loading">正在加载...</div>

      <div v-if="error" class="error">
        {{ error }}
      </div>

      <div v-if="dataList" class="content">
        <el-card>
          <Item
            v-for="(data, index) in dataList"
            :key="index"
            :data="data"
            :index="index + 1"
          ></Item>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import Item from "./AdvSearchItem";
import api from "@/api/index.js";

export default {
  name: "AdvContent",
  components: { Item },
  data() {
    return {
      dataList: "",
      isLoading: false,
      error: null,
    };
  },
  methods: {
    // 从后端得到数据
    fetchData() {
      if (this.$route.query.term === undefined) {
        return;
      }
      this.error = this.dataList = null;
      this.isLoading = true;
      api.databaseApi
        .selectAll(["PMCID"], this.$route.query.term)
        .then((response) => {
          this.dataList = response.data.data;
          this.isLoading = false;
          console.log(response);
        })
        .catch((error) => {
          this.isLoading = false;
          alert(error.message);
        });
    },
  },
  watch: {
    // 如果路由有变化，会再次执行该方法
    $route: "fetchData",
  },
  activated() {
    this.fetchData();
  },
};
</script>   